<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>发布评论</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        .comment-box {
            width: 500px;
            margin: 20px auto;
        }
        h1{
            font-size: 18px;
            margin-bottom: 5px;
        }
        .pub-comment {
            text-align: right;
        }
        .pub-comment textarea {
            display: block;
            width: 100%;
            height: 50px;
            padding: 5px;
        }
        .btns {
            display: flex;
            justify-content: flex-end;
            align-items: center;
            margin-top: 10px;
        }
        .content-count {
            margin-right: 5px;
        }
        .content-count span {
            color: red;
        }
        .pub-comment button {
            background: #0086ff;
            border: none;
            color: #fff;
            padding: 5px 10px;
        }
        .pub-comment button:hover{
            background-color: #4caafc;
            cursor: pointer;
        }
        .comment-list {
            margin-top: 10px;
        }
        .comment-item {
            display: flex;
            border-bottom: 1px dashed #ccc;
            padding: 10px 0;
            margin: 10px 0;
        }
        .comment-item .left {
            width: 50px;
            height: 50px;
            line-height: 50px;
            background-color: #eee;
            border-radius: 50%;
            text-align: center;
            margin-right: 15px;
        }
        .right .date {
            font-size: 12px;
            color: #999;
            margin-top: 5px;
        }
        .right .item-content {
            margin-top: 20px;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <div class="comment-box">
        <h1>评论</h1>
        <div class="pub-comment">
            <textarea class="content"></textarea>
            <div class="btns">
                <div class="content-count">
                    <span>0</span>/200
                </div>
                <button class="pub-btn">发布评论</button>
            </div>
        </div>
        <div class="comment-list">
            <div class="comment-item">
                <div class="left">
                    游
                </div>
                <div class="right">
                    <p class="author">游客</p>
                    <p class="date">2022/2/25 11:28:23</p>
                    <p class="item-content">
                        祝大家学业有成!
                    </p>
                </div>
            </div>
        </div>
    </div>
    <script>
        // 功能一：统计用户输入发布评论内容数字显示在评论框下方，并控制字数不能超过200字。
        let content = document.querySelector(".pub-comment .content")
        let count = document.querySelector(".content-count span")
        let pubBtn = document.querySelector(".pub-btn")
        content.addEventListener("input", function(){
            let length = content.value.length
            if(length >= 200){
                content.value = content.value.slice(0, 200)
            }
            count.innerHTML = content.value.length
        })

        // 功能二：默认list评论列表的渲染
        let list = [ 
            {
                id: 1,
                content: "祝大家学业有成!",
                date: "2024/12/12 12:12:12"
            },{
                id: 2,
                content: "祝大家幸福快乐!",
                date: "2024/12/120 12:50:12"
            }]
        function renderList(data){
            let allComment = ""
            data.forEach(item=>{
                allComment += `
                    <div class="comment-item">
                        <div class="left">
                            游
                        </div>
                        <div class="right">
                            <p class="author">游客</p>
                            <p class="date">${item.date}</p>
                            <p class="item-content">
                                ${item.content}
                            </p>
                        </div>
                    </div>
                `
            })
            document.querySelector(".comment-list").innerHTML = allComment
        }
        renderList(list)
        
        // 功能三：实现发布评论功能
        pubBtn.addEventListener("click", function(){
            let id = list.length == 0?1:list[list.length - 1].id+1;
            list.push({
                id,
                content: content.value,
                date: new Date().toLocaleString()
            })
            renderList(list)
            content.value = ""
            count.innerHTML = 0
        })


    </script>
</body>
</html>